<script>
export default {
  data() {
    return {
      dialogVisible: false, // 照片墙弹窗是否显示
      dialogImageUrl: '', // 照片墙路径
      spu: {
        category3Id: 0, // 三级分类id
        description: '', // 描述
        spuImageList: [], // spu图片
        spuName: '', // spu名称
        tmId: 0, // 平台id
        spuSaleAttrList: [
        ], // 平台属性与属性值的数组
        inputValue: '', // el-tag中输入框双向绑定的数据
        inputVisible: false // el-tag中按钮与表单谁显示谁隐藏
      }, // 存储spu信息属性
      tradeMarkList: [], // 存储品牌信息
      spuImageList: [], // 存储spu的图片信息
      saleList: [], // 存储spu的销售信息
      attrId: '' // 收集未选择的销售属性的id
    }
  },
  computed: {
    useSaleAttrList() {
      // 平台总共三个属性：saleList中
      // 商品自己有的属性：spu.spuSaleAttrList中
      // filter过滤需要的数据，every用于筛选每一个数据是否符合条件，返回布尔值，如果为真，则被filter过滤出来
      const newList = this.saleList.filter(item => {
        return this.spu.spuSaleAttrList.every(item2 => {
          return item.name !== item2.saleAttrName
        })
      })
      return newList
    }
  },
  methods: {
    async addSpu(id) {
      console.log(id)
      // 获取品牌信息
      const tradeRes = await this.$API.spu.getSpuTrade(id)
      // console.log(tradeRes);
      if (tradeRes.code === 200) {
        this.tradeMarkList = tradeRes.data
      }
      // 获取spu销售信息
      const saleRes = await this.$API.spu.getSpuSale()
      console.log(saleRes)
      if (saleRes.code === 200) {
        this.saleList = saleRes.data
      }
    },
    // 修改保存
    async editSpuFn() {
      this.spu.spuImageList = this.spuImageList.map(item => {
        return {
          imgUrl: (item.response && item.response.data) || item.url,
          imgName: item.name
        }
      })
      if (this.category3Id) {
        const res = await this.$API.spu.editSpu(this.spu)
        console.log(res)
      } else {
        await this.$API.spu.addSpu(this.spu)
      }
      this.$emit('goBack', 0)
    },
    // 点击添加销售属性按钮
    addSaleAttr() {
      const [baseSaleAttrId, saleAttrName] = this.attrId.split(':')
      this.attrId = ''
      const newAttrObj = { baseSaleAttrId, saleAttrName, spuSaleAttrValueList: [] }
      this.spu.spuSaleAttrList.push(newAttrObj)
    },
    // el-tag中按钮的点击事件
    showInput(row) {
      this.$set(row, 'inputVisible', true)
      this.$set(row, 'inputValue', '')
    },
    // el-tag中输入框的失焦或回车事件
    handleInputConfirm(row) {
      if (!row.inputValue) return

      const bool = row.spuSaleAttrValueList.some(item => item.saleAttrValueName === row.inputValue)
      if (bool) return

      const { baseSaleAttrId, inputValue } = row
      const newObj = {
        baseSaleAttrId,
        saleAttrValueName: inputValue
      }
      row.spuSaleAttrValueList.push(newObj)
      row.inputVisible = false
    },
    // 图片新增函数
    handleSuccess(response, file, fileList) {
      this.spuImageList = fileList
    },
    // 图片删除函数
    handleRemove(file, fileList) {
      this.spuImageList = fileList
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    // 获取spu数据
    async getSpuFn(spu) {
      // 获取spu信息的数据
      const spuRes = await this.$API.spu.getSpuItem(spu.id)
      // console.log(spuRes);
      if (spuRes.code === 200) {
        this.spu = spuRes.data
      }
      // 获取品牌信息
      const tradeRes = await this.$API.spu.getSpuTrade(spu.id)
      // console.log(tradeRes);
      if (tradeRes.code === 200) {
        this.tradeMarkList = tradeRes.data
      }
      // 获取spu图片信息
      const imageRes = await this.$API.spu.getSpuImage(spu.id)
      // console.log(imageRes);
      if (imageRes.code === 200) {
        const listArr = imageRes.data
        // 照片墙需要显示图片名字和路径，因此要做处理
        listArr.forEach(item => {
          item.name = item.imgName
          item.url = item.imgUrl
        })
        this.spuImageList = listArr
      }
      // 获取spu销售信息
      const saleRes = await this.$API.spu.getSpuSale()
      console.log(saleRes)
      if (saleRes.code === 200) {
        this.saleList = saleRes.data
      }
    }
  }
}
</script>

<template>
  <div>
    <el-form ref="form" label-width="80px">
      <el-form-item label="SPU名称">
        <el-input v-model="spu.spuName" placeholder="SPU名称" />
      </el-form-item>
      <el-form-item label="品牌">
        <el-select v-model="spu.tmId" placeholder="请选择品牌">
          <el-option v-for="item in tradeMarkList" :key="item.id" :label="item.tmName" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="SPU描述">
        <el-input v-model="spu.description" type="textarea" rows="4" placeholder="SPU描述" />
      </el-form-item>
      <el-form-item label="SPU图片">
        <el-upload
          action="/dev-api/admin/product/fileUpload"
          list-type="picture-card"
          :file-list="spuImageList"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove"
          :on-success="handleSuccess"
        >
          <i class="el-icon-plus" />
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
      </el-form-item>
      <el-form-item label="销售属性">
        <el-select v-model="attrId" :placeholder="`还有${useSaleAttrList.length}未选择`">
          <el-option v-for="item in useSaleAttrList" :key="item.id" :label="item.name" :value="`${item.id}:${item.name}`" />
        </el-select>
        <el-button :disabled="!attrId" type="primary" icon="el-icon-plus" @click="addSaleAttr">添加销售属性</el-button>
        <!-- 展示的是当前spu属于自己的销售属性 -->
        <el-table :data="spu.spuSaleAttrList" border style="margin:20px 0; width: 100%">
          <el-table-column type="index" label="序号" width="100px" align="center" />
          <el-table-column prop="saleAttrName" label="属性名" />
          <el-table-column prop="spuSaleAttrValueList" label="属性值名称列表">
            <template v-slot="{row,$index}">
              <el-tag v-for="item in row.spuSaleAttrValueList" :key="item.id" closable :disable-transitions="false" @close="row.spuSaleAttrValueList.splice($index,1)">{{ item.saleAttrValueName }}</el-tag>
              <el-input
                v-if="row.inputVisible"
                ref="saveTagInput"
                v-model="row.inputValue"
                class="input-new-tag"
                size="small"
                @keyup.enter.native="handleInputConfirm(row)"
                @blur="handleInputConfirm(row)"
              />
              <el-button v-else class="button-new-tag" size="small" @click="showInput(row)">添加</el-button>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template v-slot="{ row, $index }">
              <el-button type="danger" size="mini" icon="el-icon-delete" @click="spu.spuSaleAttrList.splice($index,1)" />
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="editSpuFn">保存</el-button>
        <el-button @click="$emit('changeScene')">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style scoped>
.el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
</style>
